package demo4;

class Animal {
    public String name;
    public int age;//成员变量
    public Animal(String name, int age) {//构造方法
        this.name = name;
        this.age = age;//初始化
        //System.out.println("Animal(String name, int age)......");
    }
    public void eat() {//普通方法
        System.out.println(name + "正在吃饭...");
    }
}

class Dog extends Animal {
    public Dog(String name, int age) {//构造方法
        super(name, age);
        //System.out.println("Dog(String name, int age)......");
    }
    public void bark() {//普通方法
        System.out.println(name + "正在汪汪叫...");
    }
    public void eat() {//子类重写/覆写/覆盖了父类的方法，方法重写
        System.out.println(name + "正在吃狗粮...");
    }
}

class Bird extends Animal {
    public Bird(String name, int age) {
        super(name, age);
        //System.out.println("Bird(String name, int age)......");
    }
    public void fly() {
        System.out.println(name + "正在飞...");
    }
    public void eat() {//子类重写/覆写/覆盖了父类的方法，方法重写
        System.out.println(name + "正在吃鸟粮...");
    }
}

public class Test {
    public static void func3(Animal animal) {
        animal.eat();
    }
    public static void main(String[] args) {
        Animal animal1 = new Dog("旺财", 2);
        func3(animal1);
        Animal animal2 = new Bird("小云", 1);
        func3(animal2);
    }



    public static void main2(String[] args) {
        Animal animal = new Dog("旺财", 2);
        animal.eat();//动态绑定（运行时绑定）
        //animal.bark();//err 向上转型之后只能通过父类引用调用父类的成员变量和方法（向上转型的缺点）
    }

    public static void func1(Animal animal) {

    }
    public static Animal func2() {
        Dog dog = new Dog("旺财", 2);
        return dog;//返回类型发生向上转型
    }
    public static void main1(String[] args) {
        Animal animal = func2();

//        Animal animal = new Animal("小动物", 1);
//        animal.eat();

//        Dog dog = new Dog("旺财", 2);
//        Animal animal = dog;

        //Animal animal = new Dog("旺财", 2);//把子类对象给到父类对象来引用（向上转型） 父类引用 引用了子类对象 直接赋值

        Dog dog = new Dog("旺财", 2);
        func1(dog);//向上转型：作为方法的参数进行传递
    }
}
